package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

    @Autowired
    private TrackInfoService trackInfoService;

    @Autowired
    private VodService vodService;


    /**
     * /api/album/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}
     * 根据声音ID+声音数量 获取下单付费声音列表
     */
    @GuiGuLogin
    @Operation(summary = "根据声音ID+声音数量 获取下单付费声音列表")
    @GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
    public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId, @PathVariable Long trackCount) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用业务层
        List<TrackInfo> list = trackInfoService.findPaidTrackInfoList(userId, trackId, trackCount);
        return Result.ok(list);
    }

    /**
     * 查询当前用户分集购买声音列表
     * /api/album/trackInfo/findUserTrackPaidList/{trackId}
     */
    @GuiGuLogin
    @Operation(summary = "查询当前用户分集购买声音列表")
    @GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
    public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId) {
        //获取用户id
        Long userId = AuthContextHolder.getUserId();
        //调用业务
        List<Map<String, Object>> mapList = trackInfoService.findUserTrackPaidList(userId, trackId);
        return Result.ok(mapList);
    }

    /**
     * 根据声音ID，获取声音统计信息
     *
     * @param trackId
     * @return
     */
    @Operation(summary = "根据声音ID，获取声音统计信息")
    @GetMapping("/trackInfo/getTrackStatVo/{trackId}")
    public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId) {
        return Result.ok(trackInfoService.getTrackStatVo(trackId));
    }


    /**
     * /api/album/trackInfo/uploadTrack
     */
    @Operation(summary = "声音上传")
    @PostMapping("/trackInfo/uploadTrack")
    public Result<Map<String, Object>> uploadTrack(MultipartFile file) {
        //调用业务层
        Map<String, Object> map = vodService.uploadTrack(file);
        return Result.ok(map);
    }


    /**
     * /api/album/trackInfo/saveTrackInfo
     */
    @Operation(summary = "保存声音")
    @PostMapping("/trackInfo/saveTrackInfo")
    public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo) {
        //调用业务层
        trackInfoService.saveTrackInfo(trackInfoVo, AuthContextHolder.getUserId());
        return Result.ok();
    }

    /**
     * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
     *
     * @param page
     * @param limit
     * @param trackInfoQuery
     * @return
     */
    @Operation(summary = " 获取当前用户声音分页信息")
    @PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
    public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Long page, @PathVariable Long limit,
                                                       @RequestBody TrackInfoQuery trackInfoQuery) {
        //设置当前的ID
        trackInfoQuery.setUserId(AuthContextHolder.getUserId());
        //设置分页的信息
        Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
        trackListVoPage = trackInfoService.findUserTrackPage(trackListVoPage, trackInfoQuery);
        return Result.ok(trackListVoPage);
    }

    /**
     * /api/album/trackInfo/getTrackInfo/{id}
     * 获取声音信息
     */
    @Operation(summary = "获取声音信息")
    @GetMapping("trackInfo/getTrackInfo/{id}")
    public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
        //调用业务层
        TrackInfo byId = trackInfoService.getById(id);
        return Result.ok(byId);
    }

    /**
     * /api/album/trackInfo/updateTrackInfo/{id}
     */
    @Operation(summary = "修改声音")
    @PutMapping("/trackInfo/updateTrackInfo/{id}")
    public Result updateTrackInfo(@PathVariable long id, @RequestBody @Validated TrackInfoVo trackInfoVo) {
        //调用业务层
        trackInfoService.updateTrackInfo(id, trackInfoVo);
        return Result.ok();
    }

    /**
     * /api/album/trackInfo/removeTrackInfo/{id}
     */

    @Operation(summary = "删除声音信息")
    @DeleteMapping("/trackInfo/removeTrackInfo/{id}")
    public Result removeTrackInfo(@PathVariable Long id) {
        trackInfoService.removeTrackInfo(id);
        return Result.ok();
    }


    /**
     * /api/album/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}
     * 查询专辑声音分页列表
     */
    @GuiGuLogin(required = false)
    @Operation(summary = "查询专辑声音分页列表")
    @GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
    public Result<Page<AlbumTrackListVo>> findAlbumTrackPage(@PathVariable Long albumId, @PathVariable Integer page,
                                                             @PathVariable Integer limit) {
        //1.获取用户ID（可以有，可能没有）
        Long userId = AuthContextHolder.getUserId();
        //2.构建MP分页对象
        Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
        pageInfo = trackInfoService.getUserAlbumTrackPage(pageInfo, userId, albumId);
        return Result.ok(pageInfo);
    }
}

